package com.university.mentalhealthplatform.studentV3.service;

import com.university.mentalhealthplatform.appModels.Test;
import com.university.mentalhealthplatform.studentV3.dto.TestDTOV3; // 导入TestDTO类
import com.university.mentalhealthplatform.studentV3.repository.TestRepositoryV3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TestServiceV3 {

    @Autowired
    private TestRepositoryV3 testRepository;

    public List<Test> findAll() {
        return testRepository.findAll();
    }

    public Test findById(Integer id) {
        return testRepository.findById(id).orElse(null);
    }

    public Test save(Test test) {
        return testRepository.save(test);
    }

    public void deleteById(Integer id) {
        testRepository.deleteById(id);
    }

    public List<TestDTOV3> getRandomTests(int count) {
    // 获取总记录数
    long total = testRepository.count();

    // 确保请求的数量不超过总记录数
    if (count > total) {
        count = (int) total;
    }

    // 获取随机测试
    List<Test> randomTests = testRepository.findRandomTests(count);
    List<TestDTOV3> dtoList = new ArrayList<>();

    // 将Test转换为TestDTO
    for (Test test : randomTests) {
        TestDTOV3 dto = new TestDTOV3();
        dto.setId(test.getId());
        dto.setTopic(test.getTopic());
        dto.setAlways(test.getAlways());
        dto.setOften(test.getOften());
        dto.setSometimes(test.getSometimes());
        dto.setHardly(test.getHardly());
        dto.setNone(test.getNone());

        dtoList.add(dto);
    }

    return dtoList;
}


    // 其他需要的业务方法
}
